package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.FileOperateUtil;
import com.fdb.efp.nls.service.dao.RscWarnAcctDetailDao;
import com.fdb.efp.nls.service.domain.RscWarnAcctDetail;
import com.fdb.efp.nls.service.facade.RscWarnAcctDetailService;
import com.fdb.efp.nls.service.vo.RscWarnAcctDetailVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 存款账户支出明细表				<p>
 * @author chenqm1
 * @date 2018-05-29
 */
@Service("rscWarnAcctDetailService")
public class RscWarnAcctDetailServiceImpl implements RscWarnAcctDetailService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(RscWarnAcctDetailServiceImpl.class);

	@Autowired
	private RscWarnAcctDetailDao rscWarnAcctDetailDao;
	
	/**
	 * 新增操作
	 */
	public int insertRscWarnAcctDetail(RscWarnAcctDetailVO inRscWarnAcctDetailVo) {
		logger.debug("当前新增数据为:"+ inRscWarnAcctDetailVo.toString());
		int num = 0;
		try {
			RscWarnAcctDetail rscWarnAcctDetail = new RscWarnAcctDetail();
			beanCopy(inRscWarnAcctDetailVo, rscWarnAcctDetail);
			num = rscWarnAcctDetailDao.insertRscWarnAcctDetail(rscWarnAcctDetail);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(RscWarnAcctDetailVO inRscWarnAcctDetailVo) {
		logger.debug("当前删除数据条件为:"+ inRscWarnAcctDetailVo);
		int num = 0;
		try {
			RscWarnAcctDetail rscWarnAcctDetail = new RscWarnAcctDetail();
			beanCopy(inRscWarnAcctDetailVo, rscWarnAcctDetail);
			num = rscWarnAcctDetailDao.deleteByPk(rscWarnAcctDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnAcctDetailVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(RscWarnAcctDetailVO inRscWarnAcctDetailVo) {
		logger.debug("当前修改数据为:"+ inRscWarnAcctDetailVo.toString());
		int num = 0;
		try {
			RscWarnAcctDetail rscWarnAcctDetail = new RscWarnAcctDetail();
			beanCopy(inRscWarnAcctDetailVo, rscWarnAcctDetail);
			num = rscWarnAcctDetailDao.updateByPk(rscWarnAcctDetail);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inRscWarnAcctDetailVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public RscWarnAcctDetailVO queryByPk(RscWarnAcctDetailVO inRscWarnAcctDetailVo) {
		
		logger.debug("当前查询参数信息为:"+ inRscWarnAcctDetailVo);
		try {
			RscWarnAcctDetail queryRscWarnAcctDetail = new RscWarnAcctDetail();
			beanCopy(inRscWarnAcctDetailVo, queryRscWarnAcctDetail);
			RscWarnAcctDetail queryRslRscWarnAcctDetail = rscWarnAcctDetailDao.queryByPk(queryRscWarnAcctDetail);
			if (Objects.nonNull(queryRslRscWarnAcctDetail)) {
				RscWarnAcctDetailVO outRscWarnAcctDetailVo = beanCopy(queryRslRscWarnAcctDetail, new RscWarnAcctDetailVO());
				logger.debug("当前查询结果为:"+ outRscWarnAcctDetailVo.toString());
				return outRscWarnAcctDetailVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public void fileDataInsertTable(File file, RscWarnAcctDetailVO clazz, String operation) {
		try {
			List<RscWarnAcctDetailVO> readTxt = FileOperateUtil.readTxt(file,clazz.getClass(),operation);
			if(readTxt != null && readTxt.size() > 0) {
				int sum = readTxt.size();
				logger.info("文件中总数据量为:"+sum);
				int batchNum = 100;
				logger.info("分批处理设置每次处理条数:"+batchNum);
				int cycleNum = sum % batchNum == 0 ? sum/batchNum : sum/batchNum +1;
				logger.info("需要执行的处理次数:" + cycleNum);
				int insertNum = 0;
				for(int i=0;i<cycleNum;i++) {
					logger.info("开始执行第"+(i+1)+"次处理");
					int start = i*batchNum; 
					int end = start + batchNum;
					logger.info("查询数据范围:"+(start+1)+"~"+end);
					List<RscWarnAcctDetailVO> subList = new ArrayList<>();
					if(end > readTxt.size()) {
						subList = readTxt.subList(start, readTxt.size());
					}else {
						subList = readTxt.subList(start, end);
					}
					for (RscWarnAcctDetailVO vo : subList) {
						RscWarnAcctDetail rscWarnAccDtl = new RscWarnAcctDetail();
						beanCopy(vo, rscWarnAccDtl);
						int num = rscWarnAcctDetailDao.insertRscWarnAcctDetail(rscWarnAccDtl);
						insertNum = insertNum + num;
					}
				}
				logger.info("文件中数据成功插入数量为:" + insertNum);
			}
		} catch (Exception e) {
			logger.error("文件数据插入表过程出错", e);
		}
	}
	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<RscWarnAcctDetailVO> queryAllOwner(RscWarnAcctDetailVO rscWarnAcctDetailVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<RscWarnAcctDetailVO> list = null;
		try {
			List<RscWarnAcctDetail> rscWarnAcctDetails = rscWarnAcctDetailDao.queryAllOwnerByPage(rscWarnAcctDetailVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ rscWarnAcctDetails.size());
			pageSet(rscWarnAcctDetails, rscWarnAcctDetailVo);
			list = (List<RscWarnAcctDetailVO>) beansCopy(rscWarnAcctDetails, RscWarnAcctDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
